﻿// <copyright file="ModConflict.cs" company="SingularityShift">
// Copyright (c) 2009 SingularityShift.com
// </copyright>
// <author>Ben McClure</author>
// <email>ben@singularityshift.com</email>
// <date>2009-08-14</date>
// <summary>The primary implementation of an IModConflict, extending AbstractModMatcher.</summary>
namespace GameLib.Games.Components
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Text.RegularExpressions;

    /// <summary>
    /// Implements IModConflict and represents a conflict regarding a mod
    /// </summary>
    public class ModConflict : AbstractModMatcher, IModConflict
    {
        /// <summary>
        /// Initializes a new instance of the ModConflict class, specifying all options
        /// </summary>
        /// <param name="owner">The IMod which owns this conflict</param>
        /// <param name="path">The path to the conflicting file(s)</param>
        /// <param name="filePattern">The file or file pattern to match</param>
        /// <param name="message">A message about this conflict</param>
        /// <param name="obsolete">Whether the matched conflicts should be considered obsolete</param>
        public ModConflict(IMod owner, string path, string filePattern, string message, bool obsolete)
        {
            Owner = owner;
            FilePath = path;
            FilePattern = filePattern;
            Message = message;
            this.Obsolete = obsolete;
        }

        /// <summary>
        /// Initializes a new instance of the ModConflict class, specifying owner mod and file pattern
        /// </summary>
        /// <param name="owner">The IMod which owns this conflict</param>
        /// <param name="filePattern">The file or file pattern to match</param>
        public ModConflict(IMod owner, string filePattern)
            : this(owner, null, filePattern, null, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModConflict class, specifying owner mod, file path, and file pattern
        /// </summary>
        /// <param name="owner">The IMod which owns this conflict</param>
        /// <param name="path">The path to the conflicting file(s)</param>
        /// <param name="filePattern">The file or file pattern to match</param>
        public ModConflict(IMod owner, string path, string filePattern)
            : this(owner, path, filePattern, null, false)
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModConflict class, specifying all strings and converting them to the correct types
        /// </summary>
        /// <param name="owner">The IMod which owns this conflict</param>
        /// <param name="path">The path to the conflicting file(s)</param>
        /// <param name="filePattern">The file or file pattern to match</param>
        /// <param name="message">A message about this conflict</param>
        /// <param name="obsolete">Whether the matched conflicts should be considered obsolete. This should be 'True' or 'False' and will be converted to a bool.</param>
        public ModConflict(IMod owner, string path, string filePattern, string message, string obsolete)
            : this(owner, path, filePattern, message, obsolete.Equals("1"))
        {
        }

        /// <summary>
        /// Initializes a new instance of the ModConflict class, specifying only the owner mod.
        /// </summary>
        /// <param name="owner">The IMod which owns this conflict</param>
        public ModConflict(IMod owner)
        {
            Owner = owner;
        }

        /// <summary>
        /// Initializes a new instance of the ModConflict class for serialization and cloning.
        /// </summary>
        public ModConflict()
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether the conflicting mod(s) (not the mod being checked for conflicts) is/are obsolete.
        /// </summary>
        public bool Obsolete
        {
            get;
            set;
        }

        /// <summary>
        /// Creates a deep copy of this conflict.
        /// </summary>
        /// <returns>An identical ModConflict object that is equal but not the same</returns>
        public override object Clone()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Flush();
            ms.Position = 0;
            return (IModConflict) bf.Deserialize(ms);
        }
    }
}